﻿using MvcApplication1.Domain;
using MvcApplication1.Helpers;
using MvcApplication1.Models.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class RegisterUserController : Controller
    {
        //
        // GET: /RegisterUser/

        public ActionResult Index()
        {
            return View("RegisterUser");
        }

        [HttpPost]
        public ActionResult Index(SignUp model)
        {
            if (ModelState.IsValid)
            {
                if (model.Password.Equals(model.PasswordAgain))
                {

                    if (!OnlineStatusHelper.loginUserList.ContainsKey(model.UserName)) 
                    {
                        OnlineStatusHelper.loginUserList.Add(model.UserName, new User(model.UserName, model.Password, "true"));
                        TempData["state"] = true;
                        model = null;
                    }
                    else
                    {
                        ModelState.AddModelError("", "User has already been registered!");
                        TempData["state"] = false;
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Password entries do not match!");
                    TempData["state"] = false;
                }
            }
            else
            {
                TempData["state"] = false;
            }

            
            return View("RegisterUser");
        }
    }
}
